package o;

import android.content.pm.PackageInfo;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.foundation.card.base.bean.AppInfoBean;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.wishlist.api.RealizedWishInfo;
import com.huawei.appgallery.wishlist.api.WishInfo;
import com.huawei.appgallery.wishlist.api.WishListResBean;
import com.huawei.appgallery.wishlist.bean.WishInfoListCache;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class ddk {
    public static int ˊ() {
        int ˋ = WishInfoListCache.ˊ().ˋ();
        if (ˋ == 0) {
            return 0;
        }
        dch.ˎ.ˋ("WishInfoOnShelfChecker", "start to get unrealized Wish status:" + ˋ);
        ResponseBean ॱ = cfw.ॱ(new dcq(1));
        if (!(ॱ instanceof WishListResBean)) {
            return 0;
        }
        WishListResBean wishListResBean = (WishListResBean) ॱ;
        if (wishListResBean.getResponseCode() != 0 || wishListResBean.getRtnCode_() != 0) {
            return 0;
        }
        int i;
        ddb.ˎ().ॱ();
        List<WishInfo> ˏ = wishListResBean.ˏ();
        if (ˏ != null) {
            i = 0;
            for (WishInfo wishInfo : ˏ) {
                if (wishInfo.ˋ() != 3) {
                    int i2;
                    String str = "";
                    if (CollectCheck.isEmpty(wishInfo.ʻ()) || wishInfo.ʻ().get(0) == null) {
                        i2 = 0;
                    } else {
                        str = ((AppInfoBean) wishInfo.ʻ().get(0)).ˏॱ();
                        i2 = ˏ(((AppInfoBean) wishInfo.ʻ().get(0)).ʻ());
                    }
                    WishInfoListCache.ˊ().ˎ(wishInfo.ˏ(), wishInfo.ˊ(), str, i2, wishInfo.ʼ(), false);
                }
                if (wishInfo.ˋ() == 5 && wishInfo.ʼ() == 1) {
                    ˋ = i + ˎ(wishInfo);
                } else {
                    ˋ = i;
                }
                i = ˋ;
            }
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "UnRealized Wise Info size:" + WishInfoListCache.ˊ().ˎ() + ",onShelf auto install size:" + i);
        } else {
            i = 0;
        }
        WishInfoListCache.ˊ().ॱ();
        return i;
    }

    private static int ˎ(@NonNull WishInfo wishInfo) {
        if (CollectCheck.isEmpty(wishInfo.ʻ())) {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "can not find appinfos");
            return 0;
        }
        AppInfoBean appInfoBean = (AppInfoBean) wishInfo.ʻ().get(0);
        if (appInfoBean == null || TextUtils.isEmpty(appInfoBean.ˏॱ())) {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "appInfo can not install.");
            return 0;
        }
        int i;
        if (ddb.ˎ().ˋ(appInfoBean.ˏॱ()).size() > 0) {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "Wish Info repeating task onShelf:" + appInfoBean.ˏॱ());
            i = 0;
        } else if (!((cdg) cdb.ॱ(cdg.class)).ॱॱ(appInfoBean.ˏॱ())) {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "Wish Info onShelf:" + appInfoBean.ˏॱ());
            RealizedWishInfo realizedWishInfo = new RealizedWishInfo();
            realizedWishInfo.ͺ(wishInfo.ˏ());
            realizedWishInfo.ॱˊ(wishInfo.ʽ());
            realizedWishInfo.ˋ(appInfoBean.ˏॱ());
            realizedWishInfo.ʼ(appInfoBean.ॱˊ());
            realizedWishInfo.ˊ(appInfoBean.ᐝ());
            realizedWishInfo.ʽ(appInfoBean.ͺ());
            realizedWishInfo.ˋ(ॱ(appInfoBean.ॱˎ()));
            realizedWishInfo.ॱ(appInfoBean.ʽ());
            realizedWishInfo.ᐝ(appInfoBean.ˊॱ());
            realizedWishInfo.ˏ(ˏ(appInfoBean.ʻ()));
            realizedWishInfo.ˏ(appInfoBean.ˋॱ());
            realizedWishInfo.ˊॱ(wishInfo.ˊ());
            realizedWishInfo.ˎ(appInfoBean.ॱॱ());
            realizedWishInfo.ॱ(appInfoBean.ˈ());
            ddb.ˎ().ˋ(realizedWishInfo);
            i = 1;
        } else if (((cdg) cdb.ॱ(cdg.class)).ˎ(appInfoBean.ˏॱ(), false, 0)) {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "Wish Info in NotRecomUpdate , abort it");
            return 0;
        } else {
            ApkUpgradeInfo ˊ = ((cdg) cdb.ॱ(cdg.class)).ˊ(appInfoBean.ˏॱ(), false, 0);
            PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(appInfoBean.ˏॱ());
            int ˏ = ˏ(appInfoBean.ʻ());
            if (ᐝ != null && ᐝ.versionCode >= ˏ) {
                dch.ˎ.ˋ("WishInfoOnShelfChecker", "installedApp versioncode is larger , abort it");
                return 0;
            } else if (ˊ == null) {
                dch.ˎ.ˋ("WishInfoOnShelfChecker", "wish info on shelf but cannot get upgrade info");
                return 0;
            } else if (ˊ.ˏॱ() == 1 || ˊ.ॱˋ() == 1) {
                dch.ˎ.ˋ("WishInfoOnShelfChecker", "different signature or lower target sdk , abort it");
                return 0;
            } else {
                i = ˏ(wishInfo, 0, appInfoBean, ˊ);
            }
        }
        return i;
    }

    private static int ˏ(@NonNull WishInfo wishInfo, int i, AppInfoBean appInfoBean, ApkUpgradeInfo apkUpgradeInfo) {
        try {
            dch.ˎ.ˋ("WishInfoOnShelfChecker", "Wish Info onShelf:" + appInfoBean.ˏॱ());
            JSONObject jSONObject = new JSONObject(apkUpgradeInfo.toJson());
            RealizedWishInfo realizedWishInfo = new RealizedWishInfo();
            realizedWishInfo.fromJson(jSONObject);
            realizedWishInfo.ͺ(wishInfo.ˏ());
            realizedWishInfo.ॱˊ(wishInfo.ʽ());
            realizedWishInfo.ˊॱ(wishInfo.ˊ());
            realizedWishInfo.ॱ(appInfoBean.ˈ());
            ddb.ˎ().ˋ(realizedWishInfo);
            return i + 1;
        } catch (IllegalAccessException e) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "ApkUpgradeInfo Clone error:" + e.getMessage());
            return i;
        } catch (JSONException e2) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "ApkUpgradeInfo Clone error:" + e2.getMessage());
            return i;
        } catch (ClassNotFoundException e3) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "ApkUpgradeInfo Clone error:" + e3.getMessage());
            return i;
        } catch (InstantiationException e4) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "ApkUpgradeInfo Clone error:" + e4.getMessage());
            return i;
        }
    }

    public static int ˏ(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Throwable e) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "num format error", e);
            return 0;
        }
    }

    public static long ॱ(String str) {
        try {
            return Long.parseLong(str);
        } catch (Throwable e) {
            dch.ˎ.ˊ("WishInfoOnShelfChecker", "num format error", e);
            return 0;
        }
    }
}
